const { defineConfig } = require('@vue/cli-service')
let bodyParser = require('body-parser');
let Mock = require('mockjs');

let data = Mock.mock({
  "list|100":[
    {
      'id':"@id",
      "img":"@image(100x100,@color)",
      title:"@ctitle(10)",
      content:"@cparagraph",
      "pricew|5-30":1,
    }
  ]
})

module.exports = defineConfig({
  transpileDependencies: true,
  devServer:{
    setupMiddlewares:(middlewares,{app})=>{
      app.use(bodyParser.json());

      // 获取商品列表
      app.get('/api/list',(req,res)=>{
        let {page,size} = req.query;
        res.json({
          code:200,
          msg:"请求成功",
          data:{
            total:data.list.length,
            list:data.list.slice(page*size,(page*1+1)*size)
          }
        })
      })

      // 级联选择的动态展示接口
      app.get('/api/cascader', (req, res) => {
        const { level, value } = req.query;//接口的传参方式
        const data = Mock.mock({
          'list|10': [{
            value: '@guid',
            label: '@ctitle(3, 5)',
            isLeaf: level >= 2,
            children: [
              {
                value: '@guid',
                label: '@ctitle(3, 5)',
                isLeaf: level >= 2,
              }
            ]
          }]
        }).list;
        if (value) {
          res.json(data.filter(item => item.value.includes(value)));
        } else {
          res.json(data);
        }
      });

      return middlewares;
    }
  }
})
